package com.twentytwok.hospitalsystem22K.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.twentytwok.hospitalsystem22K.common.Code;
import com.twentytwok.hospitalsystem22K.common.R;
import com.twentytwok.hospitalsystem22K.mapper.UserMapper;
import com.twentytwok.hospitalsystem22K.pojo.User;
import com.twentytwok.hospitalsystem22K.pojo.UserExample;
import com.twentytwok.hospitalsystem22K.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


@Service("userService")
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public List<User> findUsers() {
        return userMapper.selectByExample(null);
    }

    @Override
    public R getPageData(Map<String, Object> params) {
        for (Map.Entry<String, Object> entry : params.entrySet()) {
            System.out.println(entry.getKey() + " " + entry.getValue());
        }

        PageHelper.startPage(Integer.parseInt(params.get("page") + ""), Integer.parseInt(params.get("limit") + ""));

        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        criteria.andDeleteEqualTo(false);

        if (params.get("name") != null && !params.get("name").equals("")) {
            criteria.andNameLike("%" + params.get("name") + "%");
        }
        if (params.get("sex") != null && !params.get("sex").equals("")) {
            criteria.andSexLike("%" + params.get("sex") + "%");
        }


        List<User> users = userMapper.selectByExample(userExample);

        PageInfo<User> userPageInfo = new PageInfo<>(users);

        return new R(Code.OK, "查询用户分页信息", (int) userPageInfo.getTotal(), userPageInfo.getList());

    }

    @Override
    public R addUser(User user) {
        int result = userMapper.insertSelectiveSelectKey(user);
        if (result>0){
            return new R(Code.OK,"添加成功",user);
        }else {
            return new R(Code.ERROR,"添加失败");
        }
    }

    @Override
    public R deleteById(Integer id) {
        User user = new User();
        user.setId(id);
        user.setDelete(true);
        int result = userMapper.updateByPrimaryKeySelective(user);
        if (result>0){
            return new R(Code.OK,"删除成功");
        } else {
            return new R(Code.ERROR,"删除失败");
        }
    }

    @Override
    public R updateById(User user) {
        int result = userMapper.updateByPrimaryKeySelective(user);
        if (result==1){
            return new R(Code.OK,"修改成功");
        }else {
            return new R(Code.ERROR,"修改失败");
        }
    }

    @Override
    public R selectUserById(Integer id) {
        User user = userMapper.selectByPrimaryKey(id);


        if (user != null){
            return new R(Code.OK,"查询成功",user);
        }else {
            return new R(Code.ERROR,"查询失败");
        }
    }

    @Override
    public R login(Integer id, String password) {

        UserExample example = new UserExample();
        example.createCriteria()
                .andIdEqualTo(id)
                .andPasswordEqualTo(password)
                .andDeleteEqualTo(false);

        List<User> users = userMapper.selectByExample(example);

        if (!users.isEmpty()) {
          return new R(Code.OK,"登录成功",users.get(0));
        }
        return new R(Code.ERROR,"id或密码错误");
    }
}
